package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.MesBarcodeinfo;
import com.futureCloudMOM.core.domain.ProduceDispatch;
import com.futureCloudMOM.core.domain.ProduceReport;
import com.futureCloudMOM.core.domain.ProduceTaskBill;
import com.futureCloudMOM.core.service.MesBarcodeinfoService;
import com.futureCloudMOM.core.service.ProduceDispatchService;
import com.futureCloudMOM.core.service.ProduceReportService;
import com.futureCloudMOM.core.mapper.ProduceReportMapper;
import com.futureCloudMOM.core.service.ProduceTaskBillService;
import com.futureCloudMOM.core.utils.UUIDUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 【Produce_Report】的数据库操作Service实现
* @author Administrator
* @since 2023-06-06 16:02:47
*/
@Service
@AllArgsConstructor
public class ProduceReportServiceImpl extends BaseServiceImpl<ProduceReportMapper, ProduceReport> implements ProduceReportService{

    // 派工单
    private final ProduceDispatchService produceDispatchService;
    // 生产订单
    private final ProduceTaskBillService produceTaskBillService;
    // mes条码
    private final MesBarcodeinfoService mesBarcodeinfoService;

    @Override
    @Transactional
    public void create(ProduceReport produceReport) {
        // 赋值报工单号
        produceReport.setReportNo(UUIDUtils.createdUUID("BG"));
        // 根据派工单ID查询派工单的生产订单ID
        ProduceDispatch produceDispatch = produceDispatchService.getById(produceReport.getDispatchID());
        // 根据派工单信息里的生产订单ID查询生产订单的任务单号
        ProduceTaskBill produceTaskBill = produceTaskBillService.getById(produceDispatch.getTaskBillId());
        // 任务单号赋值
        produceReport.setTaskBillNo(produceTaskBill.getTaskBillNo());
        // 判断本次报工是否需要审核  0:是  1:否
        if(produceReport.getWhetherCheck() == 1){
            produceReport.setStatus(1L);
            // 不需要审核 派工单信息更新
            produceDispatchService.reportWrite(produceReport);
            // 新增报工
            save(produceReport);
            // 按标包生成条码
            mesBarcodeinfoService.createdMesBarcode(produceReport);
        }else{
            produceReport.setStatus(0L);
            // 新增报工
            save(produceReport);
        }
    }

    @Override
    @Transactional
    public void reportCheck(Long id) {
        // 根据报工单ID查询报工信息
        ProduceReport report = getById(id);
        if(report.getStatus() != 0){
            throw new BaseException("待审核状态的报工单才可进行审核操作");
        }else{
            // 状态改为已审核
            report.setStatus(1L);
            // 按标包生成条码
            mesBarcodeinfoService.createdMesBarcode(report);
            // 更新报工信息
            updateById(report);
            // 审核完成 派工单信息更新
            produceDispatchService.reportWrite(report);
        }
    }

    @Override
    @Transactional
    public void reverseCheck(Long id) {
        // 根据报工单ID查询报工信息
        ProduceReport report = getById(id);
        // 报工单的状态不是已审核 并且入库状态不是待入库 报错给出提示
        if(report.getStatus() != 1 && report.getForceStopIn() != 0){
            throw new BaseException("报工单状态是已审核,并且是待入库的报工单,才可进行反审核操作");
        }else{
            // 状态改为反审核
            report.setStatus(0L);
            // 更新报工信息
            updateById(report);
            // 反审核完成 派工单信息更新
            // 根据id获取派工单信息
            ProduceDispatch produceDispatch = produceDispatchService.getById(report.getDispatchID());
            // 报工数量扣除
            // 合格
            produceDispatch.setReportQty(produceDispatch.getReportQty().subtract(report.getQty()));
            // 不良
            produceDispatch.setReworkQty(produceDispatch.getReworkQty().subtract(report.getReworkQty()));
            // 报废
            produceDispatch.setScrapQty(produceDispatch.getScrapQty().subtract(report.getScrapQty()));
            // 派工单状态改为生产中
            produceDispatch.setStatus(1L);
            produceDispatchService.updateById(produceDispatch);
            // 审核生成的MES条码删除
            mesBarcodeinfoService.list(new LambdaQueryWrapper<MesBarcodeinfo>()
            .eq(MesBarcodeinfo::getReportid,id)
            .eq(MesBarcodeinfo::getDeleted,0)).forEach(item ->{
                mesBarcodeinfoService.removeById(item.getId());
            });
        }
    }

    @Override
    public void cancellation(Long id) {
        // 根据报工单ID查询报工信息
        ProduceReport report = getById(id);
        if(report.getStatus() != 0){
            throw new BaseException("待审核状态的报工单才可进行作废操作");
        }else{
            // 报工单状态改为已作废
            report.setStatus(2L);
            // 更新报工单信息
            updateById(report);
        }
    }

    @Override
    public void alteration(ProduceReport produceReport) {
        // 根据ID或者报工单最新数据
        ProduceReport produceReport1 = getById(produceReport.getId());
        // 判断状态是否是待审核
        if(produceReport1.getStatus() != 0){
            throw new BaseException("待审核的报工单才可以进行变更操作");
        }else{
            // 更新变更信息
            updateById(produceReport);
        }
    }

}
